package com.otod.service;

import com.otod.entity.*;
import com.otod.repository.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用户管理类.
 *
 * @author calvin
 */
// Spring Service Bean的标识.
@Component
@Transactional
public class StockService {


    private static Logger logger = LoggerFactory.getLogger(StockService.class);

    @Autowired
    private StockDao stockDao;

    @Autowired
    private QuoteDao quoteDao;

    @Autowired
    private TradeDao tradeDao;

    @Autowired
    private MinDao minDao;

    @Autowired
    private TraderDao traderDao;

    public List<Stock> getAllStock() {
        return (List<Stock>) stockDao.findAll();
    }

    public List<Trader> getAllTrader() {
        return (List<Trader>) traderDao.findAll();
    }

    public List<Trader> getAllTraderEx(){
        return (List<Trader>) traderDao.findAll();
    }

    public List<Trade> getAllTradeByCode(String code) {
        return (List<Trade>) tradeDao.findByCode(code);
    }


    public List<Trader> getAllTraderOrderByCurrAmount() {
        return (List<Trader>) traderDao.findAllTrader().subList(0,5);
    }


    public void saveStock(Stock stock) {
        stockDao.save(stock);
    }

    public Stock getByCode(String code) {
        return stockDao.findByCode(code);
    }

    public Quote getLastestQuoteByStock(String code) {
        return quoteDao.findByCode(code);
    }

    public List<Min> getMins(String code) {
        return minDao.findByCode(code);
    }

    @Transactional(readOnly = false)
    public void saveTrade(Trade trade, Quote quote, Stock stock) {
        //更新股票最新价格
        stock.setPrice(trade.getPrice());
        //更改股票最新数量
        if (StringUtils.equals(trade.getType(), "B")) {
            stock.setSurVolume(stock.getSurVolume() - trade.getTradeCount());
        } else {
            stock.setSurVolume(stock.getSurVolume() + trade.getTradeCount());
        }
        Min min = minDao.findByCodeAndTradeTime(trade.getCode(), StringUtils.substring(trade.getTradeTime(), 0, 12));
        if (min == null) {
            min = new Min();
            min.setCode(trade.getCode());
            min.setTradeTime(StringUtils.substring(trade.getTradeTime(), 0, 12));
            min.setVolume(0);
            min.setAmount(BigDecimal.valueOf(0));
        }

        min.setPrice(trade.getPrice());
        min.setVolume(min.getVolume() + trade.getTradeCount());
        min.setAmount(min.getAmount().add(trade.getPrice().multiply(new BigDecimal(trade.getTradeCount().intValue()))));
        //更新股票交易人的信息
        Trader trader = traderDao.findByCode(trade.getTrader());
        if (trader != null) {
            if (StringUtils.equals(trade.getType(), "B")) {
                trader.setCurrAmount(trader.getCurrAmount().subtract(trade.getPrice().multiply(new BigDecimal(trade.getTradeCount().intValue()))));
            } else {
                trader.setCurrAmount(trader.getCurrAmount().add(trade.getPrice().multiply(new BigDecimal(trade.getTradeCount().intValue()))));
            }
        }
//        Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
//        for(Trade t : tradeDao.findByCode(trade.getCode())){
//            if(map.get(t.getTrader()) == null){
//                map.put(t.getTrader(),BigDecimal.ZERO);
//            }
//            BigDecimal amount = map.get(t.getTrader());
//            if(StringUtils.equals(t.getType(),"B")){
//
//            }
//        };
        stockDao.save(stock);
        minDao.save(min);
        tradeDao.save(trade);
        traderDao.save(trader);
    }
}
